package com.jwthhealth_pub.old.report.view.adapter;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.report.model.resbody.HraDataRes;
import com.jwthhealth_pub.old.report.view.IndicalEnvironmentActivity;
import com.jwthhealth_pub.old.report.view.ReportEnvironmentActivity;

import java.util.ArrayList;
import java.util.List;

public class ReportEnvironmentAdapter extends RecyclerView.Adapter {

//    private String[] colors = new String[]{"#ffc5e7", "#e4a6d5", "#f28485",
//            "#f170a6", "#ec75d7", "#a370ec", "#76a9fe", "#67d7ec", "#65e1e0", "#dafd71", "#7ae25a", "#66d99e"};

      private String[] colors = new String[]{"#6bcbca", "#fe9b48", "#dafd71",
            "#f170a6", "#be8059", "#a370ec", "#76a9fe", "#85414e", "#65e1e0", "#f28485", "#29570f", "#66d99e"};

    private ReportEnvironmentActivity mActivity;
    private int mIndex;
    private HraDataRes.DataBean.NhjBean.SonBeanXXX mData;
    private List<HraDataRes.DataBean.NhjBean.SonBeanXXX.SonBeanXX> mShowData;

    public ReportEnvironmentAdapter(ReportEnvironmentActivity reportEnvironmentActivity, int index, HraDataRes.DataBean.NhjBean.SonBeanXXX mData) {
        this.mIndex = index;
        this.mActivity = reportEnvironmentActivity;
        this.mData = mData;

        List<HraDataRes.DataBean.NhjBean.SonBeanXXX.SonBeanXX> son = mData.getSon();
        mShowData = new ArrayList<>();

        int _size;
        if (son.size() >= 5) _size = 5;
        else _size = son.size();
        for (int i = 0; i < _size; i++) {
            mShowData.add(son.get(i));
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mActivity, R.layout.item_report_physical_describe, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final ViewHolder vh = (ViewHolder) holder;
        Resources resources = mActivity.getResources();

        if (position == 0) {
            vh.startLayout.setVisibility(View.GONE);
            vh.startTv.setVisibility(View.VISIBLE);
            vh.startTitleTv.setText(resources.getString(R.string.reprot_physical_describe_start));
            vh.middleTv.setText(resources.getString(R.string.reprot_physical_describe_middle));
            vh.endTv.setText(resources.getString(R.string.reprot_physical_describe_end));
//            int color = mActivity.getResources().getColor(R.color.primarycolor);
            int color = ContextCompat.getColor(mActivity, R.color.primarycolor);
            vh.startTitleTv.setTextColor(color);
            vh.middleTv.setTextColor(color);
            vh.startTv.setTextColor(color);
            vh.endTv.setTextColor(color);
            vh.arrowIndex.setVisibility(View.INVISIBLE);
            vh.leftArrow.setVisibility(View.INVISIBLE);
        } else if (position == getItemCount() - 1 && getItemCount() >= 7) {
            //展开，收起
            vh.arrowIndex.setVisibility(View.VISIBLE);
            if (getItemCount() > 7) {
                vh.arrowIndex.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.show_preview_arrow));
            } else {
                vh.arrowIndex.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.show_more_arrow));
            }

            vh.arrowLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getItemCount() == 7) {
                        shwoMoreData();
                    } else {
                        showPreviewData();
                    }
                }
            });
        } else {
            if (position > mData.getSon().size()) return;
            final HraDataRes.DataBean.NhjBean.SonBeanXXX.SonBeanXX sonBeanXX = mData.getSon().get(position - 1);

            vh.leftArrow.setVisibility(View.VISIBLE);

            if (mIndex == 1)
                vh.startIv.setVisibility(View.GONE);

            vh.startLayout.setVisibility(View.VISIBLE);
//            vh.startTitleTv.setVisibility(ViewHolder.GONE);
            int color = ContextCompat.getColor(mActivity, R.color.textColorBlack);
//            int color = mActivity.getResources().getColor(R.color.textColorBlack);
            vh.middleTv.setTextColor(color);
            vh.startTv.setTextColor(color);
            // 数据，循环
            vh.startTv.setText(sonBeanXX.getC_name());
            vh.startIv.setBackgroundColor(Color.parseColor(colors[position - 1]));
            vh.middleTv.setText(sonBeanXX.getLook_value());
            vh.endTv.setText(sonBeanXX.getValue());
            vh.startTv.setTextColor(color);
            vh.endTv.setTextColor(color);
            vh.startTitleTv.setTextColor(color);
            vh.middleTv.setTextColor(color);
            vh.arrowIndex.setVisibility(View.GONE);

            vh.leftArrowLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    gotoDetil(sonBeanXX.getCompare_id());
                }
            });

            vh.middleTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    gotoDetil(sonBeanXX.getCompare_id());
                }
            });
        }
    }

    private void gotoDetil(String id){
        // TODO: 2017/10/23 指标说明
        Intent intent = new Intent(mActivity, IndicalEnvironmentActivity.class);
        intent.putExtra("id", id);
        mActivity.startActivity(intent);
    }

    private void showPreviewData() {
        mShowData.clear();
        for (int i = 0; i < 5; i++) {
            mShowData.add(mData.getSon().get(i));
            notifyDataSetChanged();
        }
    }

    private void shwoMoreData() {
        mShowData.clear();
        if (mData == null) return;
        if (mData.getSon() == null) return;
        for (int i = 0; i < mData.getSon().size(); i++) {
            mShowData.add(mData.getSon().get(i));
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        if (mData.getSon().size() <= 5) {
            return mShowData.size() + 1;
        } else {
            return mShowData.size() + 2;
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout startLayout;
        private ImageView startIv, arrowIndex;
        private TextView startTitleTv, startTv, middleTv, endTv;
        private RelativeLayout arrowLayout;
        private RelativeLayout leftArrowLayout;
        private ImageView leftArrow;

        ViewHolder(View itemView) {
            super(itemView);
            startLayout = itemView.findViewById(R.id.layout_start);
            startTitleTv = itemView.findViewById(R.id.tv_start_title);
            startTv = itemView.findViewById(R.id.tv_start);
            middleTv = itemView.findViewById(R.id.tv_middle);
            endTv = itemView.findViewById(R.id.tv_end);
            startIv = itemView.findViewById(R.id.iv_start);
            arrowIndex = itemView.findViewById(R.id.arrow_idex);
            arrowLayout = itemView.findViewById(R.id.arrow_icon_layout);
            leftArrowLayout = itemView.findViewById(R.id.left_arrow_layout);
            leftArrow = itemView.findViewById(R.id.left_arrow);
        }
    }
}
